## Template for typical peertool controller. To use, read this file into a new one and
## globally replace 'controller' with the file name.

@auth.requires_login()
def index():
    """ Please describe."""
    debug("Entering ...")

    ## Uncomment if this function will be called
    ## by redirection with a new choice of problem.

    #if request.args and request.args[0] != None:
    #    session.problemid = int(request.args[0])

    problem = loadProblem(session.problemid)
    if problem==None:
        redirect(URL(r=request, c='choose', f='index'))

    form = controllerform(problem)    
    if form.accepts(request.vars, session):
        session.flash = T('Form accepted.')
        #import pdb; pdb.set_trace()
        ## Process submitted data here

        ## Optionally redirect to another page
        #redirect(URL(r=request, f='somecontroller'))

    elif form.errors:
        response.flash = T('Form has errors!')

    else:
        response.flash = T('Fill out the form')
    debug("Exiting ...")    
    return dict(form=form, status="Unimplemented Controller")  

def controllerformstyle(id,a,td_b,c,styleitem=None):
    """ Some typical styleitem names and handlers. Note that
        styleitems aren't limited to strings. They can be arbitrary
        objects with whatever attributes and methods you wish 
        to define."""
    #import pdb; pdb.set_trace()
    if styleitem == 'labelright':
        ##  Remove the colon and 
        ##  put the label to the right of the slider
        a.components[1] == ''
        row = TR(td_b, TD(a,_class='w2p_fl'),
                     TD(c,_class='w2p_fc'),_id=id)   

    elif styleitem == 'formtitle':
        title =  H1('Do something with: "%s"'%''.join(td_b.components))
        row = TR(TD(title,_class='w2p_fl', _colspan=3))
            
    elif styleitem == 'sectiontitle':
        title =  H2('%s'%''.join(td_b.components))
        row = TR(TD(title,_class='w2p_fl', _colspan=3)) 

    elif styleitem == 'delbox':
        row = TR(TD(c,_class='w2p_fw',_colspan=2), td_b,_id=id) 

    else:
        ## mimic formstyle 'table3cols'
        row = TR(TD(a,_class='w2p_fl'), td_b,
                     TD(c,_class='w2p_fc'),_id=id)
    return row  


def controllerform(problem):
    fields = []
    fields.append(Field("Title",'string',
                   writable=False,
                   readable=True,
                   default=ptproblem.mkLabel(problem.text),
                   #label = 'Do something with',
                   formstyleitem='formtitle',))
    myform = SQLFORM.factory(*fields, formstyle=controllerformstyle)
    return myform
